package com.pool.activity;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.pool.R;
import com.pool.activity.common.FullscreenListActivity;
import com.pool.common.Lists;
import com.pool.common.Maps;
import com.pool.common.Record;
import com.pool.common.Shoot;
import com.pool.common.Strings;
import com.pool.store.RecordStore;
import com.pool.store.ShootStore;
import com.pool.store.Store;

/**
 * Shows a list of all the shots for a given player.
 * 
 * @author claveld@gmail.com (David Clavel)
 */
public class History extends FullscreenListActivity {

  private static final String FILTER = "{filter}";
  
  private final Store<Record> permanentRecordStore;
  private final Store<Shoot> shootStore;

  public History() {
    this(RecordStore.usingPermanentTable(), new ShootStore());
  }
  
  public History(Store<Record> permanentRecordStore, Store<Shoot> shootStore) {
    this.permanentRecordStore = permanentRecordStore;
    this.shootStore = shootStore;
  }
  
  public static Intent createIntentWithFilter(Context context, String filter) {
    Intent result = new Intent(context, History.class);
    result.putExtra(FILTER, filter);
    return result;
  }
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.local_store);

    String filter = getIntent().getExtras().getString(FILTER);
    setTitle(Strings.getString(R.string.history).replace(FILTER, filter));
    
    List<Map<String, Object>> myData = Lists.newArrayList();

    for (Record record : permanentRecordStore.getAll()) {
      if (Strings.isEmpty(filter) || filter.equals(record.player)) {
        
        // TODO(claveld): extract this to a common place (used in ViewLocalStore).
        Shoot shoot = shootStore.get(record.shootId);
        String suffix = "";
        if (shoot.isVictory()) {
          if (record.remainingBalls < 0) {
            suffix = "";
          } else if (record.remainingBalls == 0) {
            suffix = " | no balls remaining";
          } else if (record.remainingBalls == 1) {
            suffix = " | 1 ball remaining";
          } else {
            suffix = " | " + record.remainingBalls + " balls remaining";  
          }
        }
        
        addItem(myData,
                shoot.description,
                record.player + ": " + record.timestamp + suffix);
      }
    }
    
    setListAdapter(new SimpleAdapter(
        this,
        myData,
        R.layout.two_line_row,
        new String[] { "line1", "line2" },
        new int[] { R.id.text1, R.id.text2 }));
    getListView().setTextFilterEnabled(true);
    getListView().setDividerHeight(2);
  }

  @Override
  protected void onListItemClick(ListView l, View v, int position, long id) {
    //Map<String, Object> map = (Map<String, Object>) l.getItemAtPosition(position);
  }

  private void addItem(List<Map<String, Object>> list, String line1, String line2) {
    Map<String, Object> map = Maps.newHashMap();
    map.put("line1", line1);
    map.put("line2", line2);
    list.add(map);
  }
}
